package asionreachel.cn.smarthomedemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.data.CDeviceItem;
import asionreachel.cn.smarthomedemo.listener.OnRecyleItemListener;

/**
 * Created by SuperMa on 2017/6/24.
 */

public class CDeviceListAdapter extends ArrayAdapter<CDeviceItem> {
    private int resourceId;
    public CDeviceListAdapter(Context context, int textViewResourceId, List<CDeviceItem> objects) {
        super(context, textViewResourceId, objects);
        this.resourceId = textViewResourceId;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        final CDeviceItem deviceItem = getItem(position);
        LinearLayout userListItem = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resourceId, userListItem, true);
        ImageView ivIcon = (ImageView)userListItem.findViewById(R.id.device_list_icon);
        TextView tvName = (TextView)userListItem.findViewById(R.id.device_list_name);
        TextView tvDeviceId = (TextView)userListItem.findViewById(R.id.device_list_id);
        LinearLayout llBack = (LinearLayout)userListItem.findViewById(R.id.device_list_back);
        //if (deviceItem.getIsSelect())
        //btIcon.setText("使用中");
        tvName.setText(deviceItem.getName());
        tvDeviceId.setText(deviceItem.getDeviceId());
        if (deviceItem.getOnLine())
            ivIcon.setImageResource( deviceItem.getIconOnline() );
        else
            ivIcon.setImageResource( deviceItem.getIconOffline() );
        llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //onRecyleItemListener.onItemClick(getPosition(deviceItem));
                //获取对应的设备信息
                int pos = getPosition(deviceItem);
                //setSelectPosition(getPosition(deviceItem));
                //Intent intent = CAllDeviceInfoList.GetInstance().getIntent(mActivity , getDeviceTag());
                //mActivity.startActivity(intent);
                onRecyleItemListener.onItemClick(pos);
            }
        });
        llBack.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = getPosition(deviceItem);
                if (onLongRecyItemListener != null)
                {
                    onLongRecyItemListener.onItemClick(position);
                    return true;
                }
                return false;
            }
        });
        return userListItem;
    }

    private OnRecyleItemListener onRecyleItemListener;
    public void setOnRecyleItemListener(OnRecyleItemListener a_onRecyleItemListener)
    {
        this.onRecyleItemListener = a_onRecyleItemListener;
    }
    private OnRecyleItemListener onLongRecyItemListener;
    public void setOnLongRecyItemListener(OnRecyleItemListener a_onLongRecyleItemListener)
    {
        this.onLongRecyItemListener = a_onLongRecyleItemListener;
    }
}
